set more off

* Generate staptial weighting matrix ww
*Requires map data:
*shp2dta using maps/admin3_poly_32, data(data/map_data) coor(data/map_coor)  replace genid(id)
use map_data, clear
spmat contiguity cobj using map_coor, id(id) replace
spmat summarize cobj
spmat getmatrix cobj ww
mata: st_matrix("ww",ww)


use ops_quantities, clear
gen cl = 10000 * prov_32_id + year
xtreg q cas_dc_pos i.year, fe cluster(cl) nonest
keep if !mi(cas_dc_pos)
tab year, gen(Yr)
drop Yr1
gen Lcas_dc_pos=l.cas_dc_pos
gen L2cas_dc_pos=l2.cas_dc_pos

eststo clear
eststo: xsmle q            Yr* , wmatrix(ww) type(2) fe
eststo: xsmle q cas_dc_pos Yr* , wmatrix(ww) type(2) fe

drop if year==2001
drop Yr2
eststo: xsmle q Lcas_dc_pos Yr* , wmatrix(ww) type(2) fe
mat l1_effect=x_effect

drop if year==2002
drop Yr3
eststo: xsmle q L2cas_dc_pos Yr* , wmatrix(ww) type(2) fe
eststo: xsmle q cas_dc_pos Lcas_dc_pos L2cas_dc_pos Yr* , wmatrix(ww) type(2) fe


esttab using tables_figs/spatial.tex, replace booktabs ///
  keep(W*Y cas_dc_pos Lcas_dc_pos L2cas_dc_pos W*cas_dc_pos W*Lcas_dc_pos W*L2cas_dc_pos) ///
  se star(* .1 ** .05 *** .01) nonotes 

*xsmle q Lcas_dc_pos Yr* if year>2001, wmatrix(ww) type(2) fe

mat li l1_effect
